<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <div class="box">
        <input type="text" value="1,2,3">输入数字求和，数字用','隔开<br>
        <button>求和</button>
        <span id="He"></span>
    </div>
    <script>
        window.onload = function(){
            var inp = document.getElementsByTagName('input')[0];
            var but = document.getElementsByTagName('button')[0];
            var He = document.getElementById('He');
            inp.onkeyup = function(){
                this.value = this.value.replace(/[^(\d)|(,)]/,"")
            }
            but.onclick = function(){
                var sum = 0;
                var inp = document.getElementsByTagName('input')[0].value.split(',')
                for(var i in inp) {
                    sum += parseInt(inp[i])
                }
                He.innerHTML = sum;
            }

        }


    </script>
    
</body>
</html>